/**
 * @desc 设置
 */
<template>
    <view class="time" v-if="!isStop">
        <nut-swipe v-for="(item, index) in time" :key="index" :style="{ margin: '10px 0' }">
            <template #left>
                <nut-button shape="square" style="height:100%" type="danger" @click="edit(index, 'startTime')">开始时间
                </nut-button>
                <nut-button shape="square" style="height:100%" type="info" @click="edit(index, 'endTime')">结束时间
                </nut-button>
            </template>
            <nut-cell :title="`序号${index + 1}:`">
                <template v-slot:link>
                    <text>{{ item.startTime }} --- {{ item.endTime }}</text>
                    <nut-inputnumber v-model="item.limit" />
                </template>
            </nut-cell>
            <template #right v-if="isAdmin">
                <nut-button shape="square" style="height:100%" type="danger" @click="deleteId(index)">删除</nut-button>
                <nut-button shape="square" style="height:100%" type="info" @click="add(index)">新增</nut-button>
            </template>
        </nut-swipe>
        <nut-datepicker v-model="currentDate" type="time" :minute-step="5" @confirm="confirm" v-model:visible="show">
        </nut-datepicker>
        <view class="time-btn">
            <nut-button v-if="!time.length && isAdmin" type="primary" @click="add(-1)">新增</nut-button>
            <nut-button v-if="time.length" type="primary" @click="save">保存</nut-button>
        </view>
        <nut-cell v-if="isAdmin" title="请选择教练" :desc="coach" @click="() => { showCoach = true }"></nut-cell>
        <nut-picker v-if="isAdmin" v-model:visible="showCoach" :columns="columns" title="教练选择" @confirm="confirmCoach">
        </nut-picker>
        <nut-dialog title="提示" content="是否保存当前信息!" v-model:visible="visible" @cancel="onCancel" @ok="onOk" />
    </view>
    <view v-else>
        你没有权限！
    </view>
</template>

<script>
import db from '../../server/index';
import Taro from '@tarojs/taro';
import { adminOpenid } from '../../utils/config'
// 管理员openid 
const openid = Taro.getStorageSync('openid');
export default {
    name: 'Time',
    data() {
        return {
            isStop: false,
            isAdmin: true,
            time: [],
            coach: '',
            coachId: '',
            columns: [],
            show: false,
            showCoach: false,
            currentDate: '',
            index: -1,
            visible: false,
            deleteList: [],
            type: 'startTime'
        }
    },
    created() {
        this.startInit()
    },
    onPullDownRefresh() {
        Taro.showLoading({
            title: '加载中',
        })
        this.startInit()
    },
    methods: {
        startInit() {
            // 判断是否是教练还是管理员
            if (adminOpenid.includes(openid)) {
                db.collection('coach').get().then(({ data = [] }) => {
                    this.columns = data.map(item => {
                        return {
                            text: item.coach,
                            value: item._id
                        }
                    })
                    this.isAdmin = true
                    this.coach = this.columns[0].text
                    this.coachId = this.columns[0].value
                    this.init();
                })
            } else {
                db.collection('coach').where({
                    openid
                }).get().then(({ data = [] }) => {
                    if (data.length) {
                        this.coachId = data[0]._id;
                        this.isAdmin = false;
                        this.init();
                    } else {
                        this.isStop = true
                        Taro.showToast({
                            title: '你没有权限！',
                            icon: 'none',
                            duration: 2000,
                            mask: true
                        })
                    }
                })
            }
        },
        confirmCoach({ selectedValue, selectedOptions }) {
            const { value, text } = selectedOptions[0]
            this.coach = text;
            this.coachId = value;
            this.init();
        },
        confirm({ selectedValue, selectedOptions }) {
            console.log(this.index,this.type)
            this.currentDate = selectedValue.join(':');
            this.time[this.index][this.type] = this.currentDate
            this.show = false
        },
        edit(index, type) {
            this.show = true
            this.index = index
            this.type = type
        },
        add(index) {
            this.index = index + 1
            this.time.splice(index + 1, 0, { startTime: '', endTime: '', limit: 3 })
        },
        deleteId(index) {
            if (this.time[index]._id) {
                // 从库中删除
                this.deleteList.push(this.time[index]._id)
            }
            this.time = this.time.filter((a, b) => b !== index)
        },
        onCancel() {
            this.visible = false
        },
        onOk() {
            this.deleteList.forEach(item => {
                db.collection('time').where({
                    _id: item,
                }).remove();
            })
            this.time.forEach((item, index) => {
                if (item._id) {
                    db.collection('time').where({
                        _id: item._id,
                    }).update({
                        data: {
                            index,
                            startTime: item.startTime,
                            endTime: item.endTime,
                            limit: item.limit,
                        }
                    }).then(() => {
                        Taro.showToast({
                            title: '修改成功!',
                            icon: 'success',
                            duration: 2000,
                            mask: true
                        })
                    })
                } else {
                    db.collection('time').add({
                        data: {
                            coachId: this.coachId,
                            startTime: item.startTime,
                            endTime: item.endTime,
                            limit: item.limit,
                            index
                        }
                    }).then(() => {
                        Taro.showToast({
                            title: '保存成功!',
                            icon: 'success',
                            duration: 2000,
                            mask: true
                        })
                    })
                }
            })
            this.deleteList = []
            this.visible = false;
        },
        init() {
            db.collection('time').where({
                coachId: this.coachId
            }).get().then(({ data = [] }) => {
                data = data.sort((a, b) => a.index < b.index ? -1 : 1)
                this.time = data
                Taro.stopPullDownRefresh();
                Taro.hideLoading();
            })
        },
        save() {
            this.visible = true
        }
    }
}
</script>

<style lang="less">
.time {
    width: 100vw;

    &-btn {
        margin-top: 20px;
        display: flex;
        justify-content: space-around;
    }
}
</style>
